﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twc.Deployment.Client.Logic.Commands.Parameters;

namespace Twc.Deployment.Client.Logic.Commands
{
    public class STSADMCommandGenerator : ICommandGenerator
    {

        public Operation Operation
        {
            get;
            set;
        }

        public string FileName
        {
            get;
            set;
        }

        public bool Override
        {
            get;
            set;
        }

        public bool Overwrite
        {
            get;
            set;
        }

        public string Url
        {
            get;
            set;
        }

        public bool Immediate
        {
            get;
            set;
        }

        public bool AllowGacDeployment
        {
            get;
            set;
        }

        public bool Force
        {
            get;
            set;
        }


        #region ICommandGenerator Members

        public string GenerateCommand()
        {
            StringBuilder command = new StringBuilder();
            parameters = new List<BaseParameter>();

            parameters.Add(new Parameters.STSADM());
            parameters.Add(new Parameters.STSADMOperation());

            switch (Operation)
            {
                case Operation.SOLUTION_ADD:
                    parameters.Add(new Parameters.STSADMAddSolution());
                    parameters.Add(new Parameters.STSADMFileName());
                    parameters.Add(new Parameters.Literal(FileName, FileName));
                    break;

                case Operation.SOLUTION_DELETE:
                    parameters.Add(new Parameters.STSADMDeleteSolution());
                    parameters.Add(new Parameters.STSADMName());
                    parameters.Add(new Parameters.Literal(FileName, FileName));
                    if (Override)
                        parameters.Add(new Parameters.STSADMOverride());
                    break;

                case Operation.SOLUTION_DEPLOY:
                    parameters.Add(new Parameters.STSADMDeploySolution());
                    parameters.Add(new Parameters.STSADMName());
                    parameters.Add(new Parameters.Literal(FileName, FileName));
                    parameters.Add(new Parameters.STSADMUrl());
                    parameters.Add(new Parameters.Literal(Url, Url));
                    if (Immediate)
                        parameters.Add(new Parameters.STSADMImmediate());
                    if (AllowGacDeployment)
                        parameters.Add(new Parameters.STSADMAllowGacDeployment());
                    if (Force)
                        parameters.Add(new Parameters.STSADMForce());
                    break;

                case Operation.SOLUTION_RETRACT:
                    parameters.Add(new Parameters.STSADMRetractSolution());
                    parameters.Add(new Parameters.STSADMName());
                    parameters.Add(new Parameters.Literal(FileName, FileName));
                    parameters.Add(new Parameters.STSADMUrl());
                    parameters.Add(new Parameters.Literal(Url, Url));
                    if (Immediate)
                        parameters.Add(new Parameters.STSADMImmediate());
                    break;

                case Operation.BACKUP:
                    parameters.Add(new Parameters.STSADMBackup());
                    parameters.Add(new Parameters.STSADMUrl());
                    parameters.Add(new Parameters.Literal(Url, Url));
                    parameters.Add(new Parameters.STSADMFileName());
                    parameters.Add(new Parameters.Literal(FileName, FileName));
                    if (Overwrite)
                        parameters.Add(new Parameters.STSADMOverwrite());
                    break;

                case Operation.RESTORE:
                    parameters.Add(new Parameters.STSADMRestore());
                    parameters.Add(new Parameters.STSADMUrl());
                    parameters.Add(new Parameters.Literal(Url, Url));
                    parameters.Add(new Parameters.STSADMFileName());
                    parameters.Add(new Parameters.Literal(FileName, FileName));
                    if (Overwrite)
                        parameters.Add(new Parameters.STSADMOverwrite());
                    break;
            }

            foreach (var parameter in parameters)
            {
                command.Append(parameter.Parameter + " ");
            }

            return command.ToString();
        }

        public IList<BaseParameter> parameters
        {
            get;
            set;
        }

        #endregion
    }
}
